use raw_dta\DPSO_return_to_OEMA_202106_nopii, clear 

gen ndi_deathdt_s = date(pn_dth_dt,"YMD")
format ndi_deathdt_s %td

gen ndi_death_myr = ym(year(ndi_deathdt_s),month(ndi_deathdt_s))

replace nid_sex = "M" if nid_sex == "1"
replace nid_sex = "F" if nid_sex == "2"

*CLEAN DEATH LOCATION
gen us_death = dth_loc_us_st_cd < "52"
gen nonus_death = !us_death & dth_loc_us_st_cd != "99"

replace dth_loc_us_st_cd = "AL" if dth_loc_us_st_cd == "01"
replace dth_loc_us_st_cd = "AK" if dth_loc_us_st_cd == "02"
replace dth_loc_us_st_cd = "AZ" if dth_loc_us_st_cd == "03"
replace dth_loc_us_st_cd = "AR" if dth_loc_us_st_cd == "04"
replace dth_loc_us_st_cd = "CA" if dth_loc_us_st_cd == "05"
replace dth_loc_us_st_cd = "CO" if dth_loc_us_st_cd == "06"
replace dth_loc_us_st_cd = "CT" if dth_loc_us_st_cd == "07"
replace dth_loc_us_st_cd = "DE" if dth_loc_us_st_cd == "08"
replace dth_loc_us_st_cd = "DC" if dth_loc_us_st_cd == "09"
replace dth_loc_us_st_cd = "FL" if dth_loc_us_st_cd == "10"
replace dth_loc_us_st_cd = "GA" if dth_loc_us_st_cd == "11"
replace dth_loc_us_st_cd = "HI" if dth_loc_us_st_cd == "12"
replace dth_loc_us_st_cd = "ID" if dth_loc_us_st_cd == "13"
replace dth_loc_us_st_cd = "IL" if dth_loc_us_st_cd == "14"
replace dth_loc_us_st_cd = "IN" if dth_loc_us_st_cd == "15"
replace dth_loc_us_st_cd = "IA" if dth_loc_us_st_cd == "16"
replace dth_loc_us_st_cd = "KS" if dth_loc_us_st_cd == "17"
replace dth_loc_us_st_cd = "KY" if dth_loc_us_st_cd == "18"
replace dth_loc_us_st_cd = "LA" if dth_loc_us_st_cd == "19"
replace dth_loc_us_st_cd = "ME" if dth_loc_us_st_cd == "20"
replace dth_loc_us_st_cd = "MD" if dth_loc_us_st_cd == "21"
replace dth_loc_us_st_cd = "MA" if dth_loc_us_st_cd == "22"
replace dth_loc_us_st_cd = "MI" if dth_loc_us_st_cd == "23"
replace dth_loc_us_st_cd = "MN" if dth_loc_us_st_cd == "24"
replace dth_loc_us_st_cd = "MS" if dth_loc_us_st_cd == "25"
replace dth_loc_us_st_cd = "MO" if dth_loc_us_st_cd == "26"
replace dth_loc_us_st_cd = "MT" if dth_loc_us_st_cd == "27"
replace dth_loc_us_st_cd = "NE" if dth_loc_us_st_cd == "28"
replace dth_loc_us_st_cd = "NV" if dth_loc_us_st_cd == "29"
replace dth_loc_us_st_cd = "NH" if dth_loc_us_st_cd == "30"
replace dth_loc_us_st_cd = "NJ" if dth_loc_us_st_cd == "31"
replace dth_loc_us_st_cd = "NM" if dth_loc_us_st_cd == "32"
replace dth_loc_us_st_cd = "NY" if dth_loc_us_st_cd == "33"
replace dth_loc_us_st_cd = "NC" if dth_loc_us_st_cd == "34"
replace dth_loc_us_st_cd = "ND" if dth_loc_us_st_cd == "35"
replace dth_loc_us_st_cd = "OH" if dth_loc_us_st_cd == "36"
replace dth_loc_us_st_cd = "OK" if dth_loc_us_st_cd == "37"
replace dth_loc_us_st_cd = "OR" if dth_loc_us_st_cd == "38"
replace dth_loc_us_st_cd = "PA" if dth_loc_us_st_cd == "39"
replace dth_loc_us_st_cd = "RI" if dth_loc_us_st_cd == "40"
replace dth_loc_us_st_cd = "SC" if dth_loc_us_st_cd == "41"
replace dth_loc_us_st_cd = "SD" if dth_loc_us_st_cd == "42"
replace dth_loc_us_st_cd = "TN" if dth_loc_us_st_cd == "43"
replace dth_loc_us_st_cd = "TX" if dth_loc_us_st_cd == "44"
replace dth_loc_us_st_cd = "UT" if dth_loc_us_st_cd == "45"
replace dth_loc_us_st_cd = "VT" if dth_loc_us_st_cd == "46"
replace dth_loc_us_st_cd = "VA" if dth_loc_us_st_cd == "47"
replace dth_loc_us_st_cd = "WA" if dth_loc_us_st_cd == "48"
replace dth_loc_us_st_cd = "WV" if dth_loc_us_st_cd == "49"
replace dth_loc_us_st_cd = "WI" if dth_loc_us_st_cd == "50"
replace dth_loc_us_st_cd = "WY" if dth_loc_us_st_cd == "51"
replace dth_loc_us_st_cd = "Puerto Rico" if dth_loc_us_st_cd == "52"
replace dth_loc_us_st_cd = "Virgin Islands" if dth_loc_us_st_cd == "53"
replace dth_loc_us_st_cd = "Guam" if dth_loc_us_st_cd == "54"
replace dth_loc_us_st_cd = "Canada" if dth_loc_us_st_cd == "55"
replace dth_loc_us_st_cd = "Cuba" if dth_loc_us_st_cd == "56"
replace dth_loc_us_st_cd = "Mexico" if dth_loc_us_st_cd == "57"
replace dth_loc_us_st_cd = "Remainder of World" if dth_loc_us_st_cd == "59"
replace dth_loc_us_st_cd = "Unknown" if dth_loc_us_st_cd == "99"

*MERGE ARMY DEATH LOCATION 
preserve 
use raw_dta/cas_unit_deploy_nopii_v3.dta, clear


keep pid incident_country incidentdt_s death
sort pid incidentdt_s
by pid: gen id_n = _n
reshape wide incident_country incidentdt_s death, i(pid) j(id_n)
tempfile cas
save `cas'
restore 
merge 1:1 pid using `cas', nogen keep(1 3)

gen cas_death_country = incident_country1 if death1 == 1
forvalues i = 1/5 {
	replace cas_death_country = incident_country`i' if death1 == `i'
}

*LABEL VARIABLES
*ENTITY DEATH CODES ARE ORDERED BASED ON POSITION ON DEATH CERTIFICATES
*RECORD DEATH CODES ARE ORDERED BASED ON CONTRIBUTION TO DEATH
forvalues x = 1/20 {
	lab var enty_axcond_`x'_ln_id_cd "LINE ON DEATH CERTIFICATE"
	lab var enty_axcond_`x'_seq_cd "POSITION IN LINE ON DEATH CERTIFICATE"
	lab var enty_axcond_`x'_ntrinj_ind_cd "NATURE OF INJURY"
	lab var rec_axcond_`x'_ntrinj_ind_cd "NATURE OF INJURY"
}

gen udrly_caus_dth_length = length(udrly_caus_dth_cd)

********************************************************
*----------------FIREARMS------------------------------*
********************************************************
*W32-W34: Accidental Firearm Discharge
*Y22-Y24: Firearm Discharge of undetermined intent
*X72-X74: Intentional Self-harm by Firearm
gen firearm = (substr(udrly_caus_dth_cd,1,1) == "W" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "319" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "35") | (substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "219" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "25") | (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "719" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "75")


gen firearm_acc = (substr(udrly_caus_dth_cd,1,1) == "W" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "319" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "35")
gen firearm_self = (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "719" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "75")
gen firearm_und = (substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "219" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "25")

*****************************************************************************
*------------------------POISONING (drug broad)-----------------------------*
*****************************************************************************
*X40-X49: Accidental Poisoning
*Y10-Y19: Poisoning of undetermined intent
*X60-X69 Intentional Self-harm by Poisoning
*F10-F19:Drug related Mental and Behavior Disorders
*K70: Alcohol Liver Disease
gen drug = (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "399" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "50") | ///
(substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "09" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "20") | ///
 (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "599" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "70") | ///
 (substr(udrly_caus_dth_cd,1,1) == "F" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "099" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "20") | ///
 substr(udrly_caus_dth_cd,1,3) == "K70" 


gen drug_acc = (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "399" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "50")
gen drug_self = (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "599" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "70")
gen drug_und = (substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "09" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "20")
gen drug_behavior = (substr(udrly_caus_dth_cd,1,1) == "F" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "099" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "20")

*------------ALCOHOL-------------*
gen alcohol = substr(udrly_caus_dth_cd,1,3) == "F15" | substr(udrly_caus_dth_cd,1,3) == "X45" | substr(udrly_caus_dth_cd,1,3) == "X65" | substr(udrly_caus_dth_cd,1,3) == "Y15" | substr(udrly_caus_dth_cd,1,3) == "K70" 

gen alcohol_acc = substr(udrly_caus_dth_cd,1,3) == "X45"
gen alcohol_self = substr(udrly_caus_dth_cd,1,3) == "X65"
gen alcohol_und = substr(udrly_caus_dth_cd,1,3) == "Y15"
gen alcohol_liver = substr(udrly_caus_dth_cd,1,3) == "K70" 
gen alcohol_behavior = substr(udrly_caus_dth_cd,1,3) == "F15"

*---------narcotics (drug narrow: narcotics, pills, psychoactive substances...)-----------------*
gen narcotic = (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "399" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "45") | (substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "09" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "15") | (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "599" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "65") | (substr(udrly_caus_dth_cd,1,1) == "F" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "099" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "15")


gen narcotic_acc = (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "399" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "45")
gen narcotic_self = (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "599" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "65")
gen narcotic_und = (substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "09" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "15")
gen narcotic_behavior = (substr(udrly_caus_dth_cd,1,1) == "F" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "099" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "15")

***********************************************************
*-----------------Motor Vehicles--------------------------*
***********************************************************
*V00-V99: Transportation Accidents, except V899 and V958
*Y32: Crashing Motor Vehicle, undetermined intent
*X82: Intentional Self-harm by Motor Vehicle
*Y85: Sequelae of Motor Vehicle Accident
gen mv = (substr(udrly_caus_dth_cd,1,1) == "V" & !((udrly_caus_dth_cd == "V958" | udrly_caus_dth_cd == "V899") & inlist(cas_death_country,"AF","IZ","KU"))) | substr(udrly_caus_dth_cd,1,3) == "Y32" | substr(udrly_caus_dth_cd,1,3) == "X82" | substr(udrly_caus_dth_cd,1,3) == "Y85" 

gen mv_acc = substr(udrly_caus_dth_cd,1,1) == "V"  & !((udrly_caus_dth_cd == "V958" | udrly_caus_dth_cd == "V899") & inlist(cas_death_country,"AF","IZ","KU"))
gen mv_self = substr(udrly_caus_dth_cd,1,3) == "X82"
gen mv_und = substr(udrly_caus_dth_cd,1,3) == "Y32"

*********************************************************
*-----------------------Suicide-------------------------*
*********************************************************
gen suicide = substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "599" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "85"
replace suicide = 1 if udrly_caus_dth_cd == "Y870"

gen suicide_plus = suicide | (substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "09" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "35") | udrly_caus_dth_cd == "Y872"

*********************************************************
*-------------------Assault (Murder?)-------------------*
*********************************************************
gen assault = (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "849" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "999") | (substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) <= "09")
replace assault = 1 if udrly_caus_dth_cd == "Y871"

********************************************************
*--------------------War (KIA)-------------------------*
********************************************************
gen war = inlist(substr(udrly_caus_dth_cd,1,3),"Y36")
replace war = 1 if udrly_caus_dth_cd == "Y891"

********************************************************
*--------------------Combat (KIA)-------------------------*
********************************************************
gen combat = inlist(substr(udrly_caus_dth_cd,1,3),"Y36") | udrly_caus_dth_cd == "Y891" | ((udrly_caus_dth_cd == "V899" | udrly_caus_dth_cd == "V958" | udrly_caus_dth_cd == "W40") & inlist(cas_death_country,"AF","IZ","KU"))

********************************************************
*--------------------ALL ACCIDENTS---------------------*
********************************************************
gen all_acc = substr(udrly_caus_dth_cd,1,1) == "V" | substr(udrly_caus_dth_cd,1,1) == "W" | (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) <="599")

gen other_acc = all_acc & !mv_acc & !drug_acc & !firearm_acc

********************************************************
*-------------------Undetermined Intent----------------*
********************************************************
gen und_intent = substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "09" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "35"
replace und_intent = 1 if udrly_caus_dth_cd == "Y872"

**********************************************************************************
*-------Other (Not drug, firearm, war, Motor Vehicle, Assault, or Suicide)-------*
**********************************************************************************
gen other = !drug & !firearm & !war & !suicide & !mv & !assault & !combat

gen death_cd3 = substr(udrly_caus_dth_cd,1,3)

foreach var of varlist firearm* drug* narcotic* alcohol* mv* suicide* assault war all_acc other_acc und_intent combat other {
    rename `var' ndi_`var'
	gen `var'_2 = ndi_`var'
}

****************************************************************************
****************************************************************************
*----------Loop through additional Codes on Death Certificate--------------*
****************************************************************************
****************************************************************************
forvalues x = 1/20 {
    replace udrly_caus_dth_cd = enty_axcond_`x'_cond_cd
	
********************************************************
*----------------FIREARMS------------------------------*
********************************************************
*W32-W34: Accidental Firearm Discharge
*Y22-Y24: Firearm Discharge of undetermined intent
*X72-X74: Intentional Self-harm by Firearm
replace firearm_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "W" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "319" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "35") | (substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "219" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "25") | (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "719" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "75")


replace firearm_acc_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "W" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "319" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "35")
replace firearm_self_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "719" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "75")
replace firearm_und_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "219" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "25")

*****************************************************************************
*------------------------POISONING (drug broad)-----------------------------*
*****************************************************************************
*X40-X49: Accidental Poisoning
*Y10-Y19: Poisoning of undetermined intent
*X60-X69 Intentional Self-harm by poison
*F10-F19:Drug related Mental and Behavior Disorders
*K70: Alcohol Liver Disease
replace drug_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "399" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "50") | (substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "09" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "20") | (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "599" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "70") | (substr(udrly_caus_dth_cd,1,1) == "F" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "099" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "20") | substr(udrly_caus_dth_cd,1,3) == "K70" 


replace drug_acc_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "399" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "50")
replace drug_self_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "599" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "70")
replace drug_und_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "09" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "20")
replace drug_behavior_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "F" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "099" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "20")

*------------ALCOHOL-------------*
replace alcohol_2 = 1 if substr(udrly_caus_dth_cd,1,3) == "F15" | substr(udrly_caus_dth_cd,1,3) == "X45" | substr(udrly_caus_dth_cd,1,3) == "X65" | substr(udrly_caus_dth_cd,1,3) == "Y15" | substr(udrly_caus_dth_cd,1,3) == "K70" 

replace alcohol_acc_2 = 1 if substr(udrly_caus_dth_cd,1,3) == "X45"
replace alcohol_self_2 = 1 if substr(udrly_caus_dth_cd,1,3) == "X65"
replace alcohol_und_2 = 1 if substr(udrly_caus_dth_cd,1,3) == "Y15"
replace alcohol_liver_2 = 1 if substr(udrly_caus_dth_cd,1,3) == "K70" 
replace alcohol_behavior = 1 if substr(udrly_caus_dth_cd,1,3) == "F15"

*---------narcotics (drug narrow: narcotics, pills, psychoactive substances...)-----------------*
replace narcotic_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "399" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "45") | (substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "09" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "15") | (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "599" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "65") | (substr(udrly_caus_dth_cd,1,1) == "F" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "099" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "15")


replace narcotic_acc_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "399" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "45")
replace narcotic_self_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "599" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "65")
replace narcotic_und_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "09" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "15")
replace narcotic_behavior_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "F" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "099" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "15")

***********************************************************
*-----------------Motor Vehicles--------------------------*
***********************************************************
*V00-V99: Transportation Accidents
*Y32: Crashing Motor Vehicle, undetermined intent
*X82: Intentional Self-harm by Motor Vehicle
*Y85: Sequelae of Motor Vehicle Accident
replace mv_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "V" & !((udrly_caus_dth_cd == "V958" | udrly_caus_dth_cd == "V899") & inlist(cas_death_country,"AF","IZ","KU"))) | substr(udrly_caus_dth_cd,1,3) == "Y32" | substr(udrly_caus_dth_cd,1,3) == "X82" | substr(udrly_caus_dth_cd,1,3) == "Y85" 

replace mv_acc_2 = 1 if substr(udrly_caus_dth_cd,1,1) == "V" & !((udrly_caus_dth_cd == "V958" | udrly_caus_dth_cd == "V899") & inlist(cas_death_country,"AF","IZ","KU"))
replace mv_self_2 = 1 if substr(udrly_caus_dth_cd,1,3) == "X82"
replace mv_und_2 = 1 if substr(udrly_caus_dth_cd,1,3) == "Y32"

*********************************************************
*-----------------------Suicide-------------------------*
*********************************************************
replace suicide_2 = 1 if substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "599" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "85"
replace suicide_2 = 1 if udrly_caus_dth_cd == "Y870"

replace suicide_plus_2 = 1 if suicide_2 == 1 | (substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "09" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "35") | udrly_caus_dth_cd == "Y872"

*********************************************************
*-------------------Assault (Murder?)-------------------*
*********************************************************
replace assault_2 = 1 if (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "849" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "999") | (substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) <= "09")
replace assault_2 = 1 if udrly_caus_dth_cd == "Y871"

********************************************************
*--------------------War (KIA)-------------------------*
********************************************************
replace war_2 = 1 if substr(udrly_caus_dth_cd,1,3) == "Y36" 
replace war_2 = 1 if udrly_caus_dth_cd == "Y891"

********************************************************
*--------------------Combat (KIA)-------------------------*
********************************************************
replace combat_2 = 1 if inlist(substr(udrly_caus_dth_cd,1,3),"Y36") | udrly_caus_dth_cd == "Y891" | ((udrly_caus_dth_cd == "V899" | udrly_caus_dth_cd == "V958" | udrly_caus_dth_cd == "W40") & inlist(cas_death_country,"AF","IZ","KU"))

********************************************************
*--------------------ALL ACCIDENTS---------------------*
********************************************************
replace all_acc_2 = 1 if substr(udrly_caus_dth_cd,1,1) == "V" | substr(udrly_caus_dth_cd,1,1) == "W" | (substr(udrly_caus_dth_cd,1,1) == "X" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) <="599")

replace other_acc_2 = all_acc_2 & !mv_acc_2 & !drug_acc_2 & !firearm_acc_2

********************************************************
*-------------------Undetermined Intent----------------*
********************************************************
replace und_intent_2 = 1 if substr(udrly_caus_dth_cd,1,1) == "Y" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) > "09" & substr(udrly_caus_dth_cd,2,udrly_caus_dth_length-1) < "35"
replace und_intent_2 = 1 if udrly_caus_dth_cd == "Y872"

**********************************************************************************
*-------Other (Not drug, firearm, war, Motor Vehicle, Assault, or Suicide)-------*
**********************************************************************************
replace other_2 = !drug_2 & !firearm_2 & !war_2 & !suicide_2 & !mv_2 & !assault_2 & !combat_2


}

foreach var of varlist firearm* drug* narcotic* alcohol* mv* suicide* assault war all_acc combat und_intent other* {
    rename `var' ndi_`var'
}

foreach var of varlist ndi_firearm* ndi_drug* ndi_narcotic* ndi_alcohol* ndi_mv* ndi_suicide* ndi_assault ndi_all_acc ndi_und_intent ndi_other* {
    replace `var' = 0 if ndi_combat == 1
}

*********************************
*-------Deaths of Despair-------*
*********************************
gen ndi_despair = ndi_firearm | ndi_drug | ndi_suicide | ndi_und_intent
gen ndi_despair_2 = ndi_firearm_2 | ndi_drug_2 | ndi_suicide_2 | ndi_und_intent_2


save created_dta/ndi_death_data_clean_v2, replace
